package com.linkage.bookshop.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView;

import com.linkage.bookshop.authentication.CrendentialSessionService;
import com.linkage.bookshop.model.Booktype;
import com.linkage.bookshop.service.inter.BookService;
import com.linkage.bookshop.service.inter.UserService;


@Controller
@RequestMapping("/*") 
public class HomeController {
	@Resource
	private UserService userService;
	
	@Resource
	private BookService bookService;
	
	@Resource
	private CrendentialSessionService crendentialSessionService;
	
	@RequestMapping(value = "/" )
	public ModelAndView gohome(HttpServletRequest request, Model model){
		ModelAndView mav = new ModelAndView("home");
		List<Booktype> types = this.bookService.listAllTypes();
		model.addAttribute("types", types);
		return mav;
	}
	
	@RequestMapping(value = "/adminHome" )
	public ModelAndView goAdminHome(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("admin_home");
		String username = crendentialSessionService.getUsername(request);
		String name =  userService.loadNickname(username);
		mav.addObject("name",name);
		return mav;
	}
	
}